package com.itheima.sh.service;

import com.itheima.sh.dao.UserMapper;
import com.itheima.sh.pojo.User;
import com.itheima.sh.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl {
    public User login(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User u=mapper.login(user);
        sqlSession.close();
        return u;
    }

    public boolean register(User user) {
        SqlSession sqlSession=null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User u=mapper.queryUserByUserName(user.getUsername());
            if(u==null){
                mapper.register(user);
                return true;
            }else{
                return false;
            }
        } finally {
            if(sqlSession!=null)
                sqlSession.close();
        }

    }
}
